//
//  CYDataAPI.m
//  cyjf
//
//  Created by tauCross on 2017/5/10.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYDataAPI.h"

#import "CYCommunication.h"
#import "CYPublicParameters.h"

#import "CYMainController.h"

#import "CYLocationManager.h"

#import <CocoaSecurity.h>
#import <CommonCrypto/CommonCryptor.h>
#import <TCJSONKit.h>
#import <AdSupport/AdSupport.h>



#define CYDataAPIStart \
NSMutableDictionary *parameters = [[CYPublicParameters standardPublicParameters] dictionary];

#define CYDataAPISetRequireParameter(macro_key, macro_value) \
parameters[macro_key] = macro_value != nil ? macro_value : @"";

#define CYDataAPIExecuteRequest(macro_url) \
[CYStandardCommunication requestWithUrl:macro_url parameters:parameters encrypt:[self.encryptAPIs containsObject:macro_url] complete:^(NSString *response) {\
CYResponse *resp = parserResponseString(response);\
if(complete)\
{\
    complete(resp);\
}\
}];



CYResponse *parserResponseString(NSString *string)
{
    NSDictionary *responseDict = string.tc_JSONObject;
    CYResponse *response = [[CYResponse alloc] init];
    if([responseDict isKindOfClass:[NSDictionary class]])
    {
        NSString *code = responseDict[@"code"];
        NSString *errorMsg = responseDict[@"errorMsg"];
        if([code isKindOfClass:[NSString class]])
        {
            response.code = code.integerValue;
            response.message = errorMsg;
            response.content = responseDict[@"result"];
        }
        else
        {
            response.code = -4;
            response.message = @"请求失败";
        }
    }
    else
    {
        response.code = -3;
        response.message = @"请求失败";
    }
    if(response.code == 21013)
    {
        if([CYPublicParameters standardPublicParameters].token.length)
        {
            response.message = @"请重新登录";
            [[[UIApplication sharedApplication] delegate] window].rootViewController = [CYMainController standardController].navigationController;
            CYStandardAuthManager.currentAuth = nil;
            [CYStandardAuthManager saveAuth];
            [[CYMainController standardController] goHome];
            [[CYMainController standardController] showLogIn];
        }
        else
        {
            [[[UIApplication sharedApplication] delegate] window].rootViewController = [CYMainController standardController].navigationController;
            response.message = @"请登录";
            CYStandardAuthManager.currentAuth = nil;
            [CYStandardAuthManager saveAuth];
            [[CYMainController standardController] showLogIn];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:CYHideAllCustomViewOnWindowNotification object:nil];
    }
    return response;
}



@implementation CYResponse

@end



@interface CYDataAPI ()

@property(nonatomic, strong)NSSet *encryptAPIs;

@end


@implementation CYDataAPI

- (instancetype)init
{
    self = [super init];
    if(self)
    {
        self.encryptAPIs = [NSSet setWithObjects:
                            @"user/getSmsCode.json",
                            @"user/login.json",
                            @"user/modifyPassword.json",
                            @"user/userAccount.json",
                            @"user/queryBindBankCard.json",
                            @"project/bindCard.json",
                            @"user/preWalletRecharge.json",
                            @"user/walletRecharge.json",
                            @"user/walletDetail.json",
                            @"user/rechargeRecord.json",
                            @"project/preInvest.json",
                            @"project/invest.json",
                            @"user/queryAllInvestDetail.json",
                            @"user/dueDetail.json",
                            @"user/preWithdrawal.json",
                            @"user/withdrawal.json",
                            @"user/withdrawalDetail.json",
                            @"user/userBillDetail.json",
                            @"project/queryUserCoupon.json",
                            @"user/queryCoupon.json",
                            @"user/couponPromptInfo.json",
                            @"project/queryRecommendProject.json",
                            @"project/queryInProgressProject.json",
                            @"project/querySellout.json",
                            @"project/detail.json",
                            @"user/projectInvestLog.json",
                            @"project/payWayList.json",
                            @"project/projectList.json",
                            nil];
    }
    return self;
}

+ (instancetype)standardDataAPI
{
    static CYDataAPI *dataAPI = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataAPI = [[CYDataAPI alloc] init];
    });
    return dataAPI;
}

- (void)user_getSmsCodeWithMobile:(NSString *)mobile
                         sms_type:(CYDataAPISMSCodeType)sms_type
                         complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"mobile", mobile)
    CYDataAPISetRequireParameter(@"sms_type", SMSCodeTypeStringWithType(sms_type))
    CYDataAPIExecuteRequest(@"user/getSmsCode.json")
}

- (void)user_registerWithMobile:(NSString *)mobile
               mobile_auth_code:(NSString *)mobile_auth_code
                       password:(NSString *)password
                       complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"mobile", mobile)
    CYDataAPISetRequireParameter(@"mobile_auth_code", mobile_auth_code)
    CYDataAPISetRequireParameter(@"password", password)
    CYDataAPIExecuteRequest(@"user/register.json")
}

- (void)user_loginWithMobile:(NSString *)mobile
                    password:(NSString *)password
                    complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"mobile", mobile)
    CYDataAPISetRequireParameter(@"password", password)
    CYDataAPIExecuteRequest(@"user/login.json")
}

- (void)user_queryBindBankCardWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"user/queryBindBankCard.json")
}

- (void)user_projectInvestLogWithId:(NSNumber *)id
                            page_no:(NSNumber *)page_no
                           complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"id", id)
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPIExecuteRequest(@"user/projectInvestLog.json")
}

- (void)user_preWalletRechargeWithAmount:(NSString *)amount
                            bank_card_no:(NSString *)bank_card_no
                                 card_no:(NSString *)card_no
                                  mobile:(NSString *)mobile
                                 pay_way:(CYDataAPIPayWay)pay_way
                               real_name:(NSString *)real_name
                                complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"amount", amount)
    CYDataAPISetRequireParameter(@"bank_card_no", bank_card_no)
    CYDataAPISetRequireParameter(@"card_no", card_no)
    CYDataAPISetRequireParameter(@"mobile", mobile)
    CYDataAPISetRequireParameter(@"pay_way", PayWayWithPayWay(pay_way))
    CYDataAPISetRequireParameter(@"real_name", real_name)
    CYDataAPIExecuteRequest(@"user/preWalletRecharge.json")
}

- (void)user_walletRechargeWithMobile_auth_code:(NSString *)mobile_auth_code
                                        pay_way:(CYDataAPIPayWay)pay_way
                                    recharge_no:(NSString *)recharge_no
                                       complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"mobile_auth_code", mobile_auth_code)
    CYDataAPISetRequireParameter(@"recharge_no", recharge_no)
    CYDataAPISetRequireParameter(@"pay_way", PayWayWithPayWay(pay_way))
    CYDataAPIExecuteRequest(@"user/walletRecharge.json")
}

- (void)user_userAccountWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"user/userAccount.json")
}

- (void)user_modifyPasswordWithMobile:(NSString *)mobile
                     mobile_auth_code:(NSString *)mobile_auth_code
                             password:(NSString *)password
                             complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"mobile", mobile)
    CYDataAPISetRequireParameter(@"mobile_auth_code", mobile_auth_code)
    CYDataAPISetRequireParameter(@"password", password)
    CYDataAPIExecuteRequest(@"user/modifyPassword.json")
}

- (void)user_logoutWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"user/logout.json")
}

- (void)user_walletDetailWithPage_no:(NSNumber *)page_no
                                type:(CYDataAPIWalletDetailType)type
                            complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPISetRequireParameter(@"type", WalletDetailTypeWithType(type))
    CYDataAPIExecuteRequest(@"user/walletDetail.json")
}

- (void)user_preWithdrawalWithBank_id:(NSNumber *)bank_id
                             complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"bank_id", bank_id)
    CYDataAPIExecuteRequest(@"user/preWithdrawal.json")
}

- (void)user_withdrawalWithAmount:(NSNumber *)amount
                          bank_id:(NSNumber *)bank_id
                         complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"amount", amount)
    CYDataAPISetRequireParameter(@"bank_id", bank_id)
    CYDataAPIExecuteRequest(@"user/withdrawal.json")
}

- (void)user_withdrawalDetailWithPage_no:(NSNumber *)page_no
                                complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPIExecuteRequest(@"user/withdrawalDetail.json")
}

- (void)user_queryAllInvestDetailWithPage_no:(NSNumber *)page_no
                                      status:(CYDataAPIInvestRecordStatus)status
                                    complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPISetRequireParameter(@"status", InvestRecordStatusWithStatus(status))
    CYDataAPIExecuteRequest(@"user/queryAllInvestDetail.json")
}

- (void)user_dueDetailWithInvest_detail_id:(NSNumber *)invest_detail_id
                                  complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"invest_detail_id", invest_detail_id)
    CYDataAPIExecuteRequest(@"user/dueDetail.json")
}

- (void)user_checkVersionWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"user/checkVersion.json")
}

- (void)user_userBillDetailWithPageNo:(NSNumber *)pageNo
                             complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"pageNo", pageNo)
    CYDataAPIExecuteRequest(@"user/userBillDetail.json")
}

- (void)user_reportSuggestWithContent:(NSString *)content
                           contactWay:(NSString *)contactWay
                             complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"content", content)
    CYDataAPISetRequireParameter(@"contactWay", contactWay)
    CYDataAPIExecuteRequest(@"user/reportSuggest.json")
}

- (void)user_queryCouponWithStatus:(CYDataAPICouponStatus)status
                           page_no:(NSNumber *)page_no
                          complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"status", CouponStatusWithStatus(status))
    CYDataAPISetRequireParameter(@"type", @2)
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPIExecuteRequest(@"user/queryCoupon.json")
}

- (void)user_couponPromptInfoWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"user/couponPromptInfo.json")
}

- (void)user_rechargeRecordWithPage_no:(NSNumber *)page_no
                              complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPIExecuteRequest(@"user/rechargeRecord.json")
}

- (void)user_userBillInfoWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"user/userBillInfo.json")
}

- (void)user_getSysStatusWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"user/getSysStatus.json")
}

- (void)user_investmentLog_addWithCode:(NSInteger)code
                                flowId:(NSString *)flowId
                             isSuccess:(BOOL)isSuccess
                                   msg:(NSString *)msg
                              complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"code", @(code))
    CYDataAPISetRequireParameter(@"flowId", flowId)
    CYDataAPISetRequireParameter(@"isSuccess", @(isSuccess))
    CYDataAPISetRequireParameter(@"msg", msg)
    CYDataAPIExecuteRequest(@"user/investmentLog/add.json")
}

- (void)message_bannerListWithPosition:(CYDataAPIBannerPosition)position
                              complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"position", BannerPositionWithPosition(position))
    CYDataAPIExecuteRequest(@"message/bannerList.json")
}

- (void)message_readAllWithType:(CYDataAPIMessageType)type
                       complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"type", MessageTypeWithType(type))
    CYDataAPIExecuteRequest(@"message/readAll.json")
}

- (void)message_unreadCountWithType:(CYDataAPIMessageType)type
                           complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"type", MessageTypeWithType(type))
    CYDataAPIExecuteRequest(@"message/unreadCount.json")
}

- (void)project_queryRecommendProjectWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"city", [CYLocationManager standardManager].city)
    CYDataAPISetRequireParameter(@"province", [CYLocationManager standardManager].province)
    CYDataAPIExecuteRequest(@"project/queryRecommendProject.json")
}

- (void)project_projectListWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"project/projectList.json")
}

- (void)project_detailWithID:(NSNumber *)id
                    complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"id", id)
    CYDataAPIExecuteRequest(@"project/detail.json")
}

- (void)project_bindCardWithBank_card_no:(NSString *)bank_card_no
                                 card_no:(NSString *)card_no
                                  mobile:(NSString *)mobile
                        mobile_auth_code:(NSString *)mobile_auth_code
                               real_name:(NSString *)real_name
                                complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"bank_card_no", bank_card_no)
    CYDataAPISetRequireParameter(@"card_no", card_no)
    CYDataAPISetRequireParameter(@"mobile", mobile)
    CYDataAPISetRequireParameter(@"mobile_auth_code", mobile_auth_code)
    CYDataAPISetRequireParameter(@"real_name", real_name)
    CYDataAPIExecuteRequest(@"project/bindCard.json")
}

- (void)project_payWayListWithAmount:(NSNumber *)amount
                          project_id:(NSNumber *)project_id
                      user_coupon_id:(NSNumber *)user_coupon_id
                            complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"amount", amount)
    CYDataAPISetRequireParameter(@"project_id", project_id)
    CYDataAPISetRequireParameter(@"user_coupon_id", user_coupon_id)
    CYDataAPIExecuteRequest(@"project/payWayList.json")
}

- (void)project_preInvestWithAmount:(NSNumber *)amount
                       bank_card_no:(NSString *)bank_card_no
                            card_no:(NSString *)card_no
                             mobile:(NSString *)mobile
                            pay_way:(NSNumber *)pay_way
                         project_id:(NSNumber *)project_id
                          real_name:(NSString *)real_name
                     user_coupon_id:(NSNumber *)user_coupon_id
                           complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"amount", amount)
    CYDataAPISetRequireParameter(@"bank_card_no", bank_card_no)
    CYDataAPISetRequireParameter(@"card_no", card_no)
    CYDataAPISetRequireParameter(@"mobile", mobile)
    CYDataAPISetRequireParameter(@"pay_way", pay_way)
    CYDataAPISetRequireParameter(@"project_id", project_id)
    CYDataAPISetRequireParameter(@"real_name", real_name)
    CYDataAPISetRequireParameter(@"user_coupon_id", user_coupon_id)
    CYDataAPIExecuteRequest(@"project/preInvest.json")
}

- (void)project_InvestWithMobile_auth_code:(NSString *)mobile_auth_code
                                   pay_way:(NSNumber *)pay_way
                               recharge_no:(NSString *)recharge_no
                            user_coupon_id:(NSNumber *)user_coupon_id
                                  complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"mobile_auth_code", mobile_auth_code)
    CYDataAPISetRequireParameter(@"recharge_no", recharge_no)
    CYDataAPISetRequireParameter(@"pay_way", pay_way)
    CYDataAPISetRequireParameter(@"user_coupon_id", user_coupon_id)
    CYDataAPIExecuteRequest(@"project/invest.json")
}

- (void)project_queryUserCouponWithAmount:(NSString *)amount
                                projectId:(NSNumber *)projectId
                                 complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    if(amount)
        CYDataAPISetRequireParameter(@"amount", amount)
    CYDataAPISetRequireParameter(@"projectId", projectId)
    CYDataAPIExecuteRequest(@"project/queryUserCoupon.json")
}

- (void)project_querySelloutWithPage_no:(NSNumber *)page_no
                               complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPIExecuteRequest(@"project/querySellout.json")
}

- (void)news_getListWithPage_no:(NSNumber *)page_no
                       complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPIExecuteRequest(@"news/getList.json")
}

- (void)news_getTopTwoListWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"news/getTopTwoList.json")
}

- (void)riskEvaluate_getRiskByTokenWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"riskEvaluate/getRiskByToken.json")
}

- (void)advertisement_getListWithPosition:(CYDataAPIAdvertisementPosition)position
                                  page_no:(NSNumber *)page_no
                                 complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"platform", @1)
    if(page_no) CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPISetRequireParameter(@"position", AdvertisementPositionWithPosition(position))
    CYDataAPISetRequireParameter(@"status", @2)
    CYDataAPIExecuteRequest(@"advertisement/getList.json")
}

- (void)advertisement_recentActivitiesWithPage_no:(NSNumber *)page_no
                                         complete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"platform", @1)
    if(page_no) CYDataAPISetRequireParameter(@"page_no", page_no)
    CYDataAPIExecuteRequest(@"advertisement/recentActivities.json")
}

- (void)advertisement_index_navWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPIExecuteRequest(@"advertisement/index/nav.json")
}

- (void)channel_saveLogWithComplete:(CYDataAPIComplete)complete
{
    CYDataAPIStart
    CYDataAPISetRequireParameter(@"city", [CYLocationManager standardManager].city)
    CYDataAPISetRequireParameter(@"province", [CYLocationManager standardManager].province)
    NSString *idfa = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
    if(idfa) CYDataAPISetRequireParameter(@"idfa", idfa)
    CYDataAPIExecuteRequest(@"channel/saveLog.json")
}

@end
